package cn.tataben.sharding.controllers;

import java.util.List;

import cn.tataben.sharding.base.PageResponse;
import cn.tataben.sharding.base.Response;
import cn.tataben.sharding.base.Constant;

import cn.tataben.sharding.domains.vos.UserVo;
import cn.tataben.sharding.services.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("users")
public class UserController {

	@Autowired private UserService userService;

	@GetMapping("/")
	public PageResponse<List<UserVo>> list(
			@RequestParam(defaultValue = "") String query, 
			@RequestParam(defaultValue = "") String order, 
			@RequestParam(defaultValue = "0") int page, 
			@RequestParam(defaultValue = "0") int rows) {
		try {
			return userService.selects(query, order, page, rows);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return new PageResponse<List<UserVo>>().fill(Constant.ERROR);
	}

	@GetMapping("/add")
	public Response add(){
		try {
			return userService.add();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return new Response().fill(Constant.ERROR);
	}

}